package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.constant.SystemConstant;
import cn.signalpha.core.tool.node.ForestNodeMerger;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.modules.shop.entity.GoodsCategory;
import cn.signalpha.modules.shop.service.GoodsCategoryService;
import cn.signalpha.modules.shop.vo.GoodsCategoryVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 包装类,返回视图层所需的字段
 */
public class GoodsCategoryWrapper extends BaseEntityWrapper<GoodsCategory, GoodsCategoryVO> {

	private static final GoodsCategoryService goodsCategoryService;

	static {
		goodsCategoryService = SpringUtil.getBean(GoodsCategoryService.class);
	}

	public static GoodsCategoryWrapper build() {
		return new GoodsCategoryWrapper();
	}

	@Override
	public GoodsCategoryVO entityVO(GoodsCategory goodsCategory) {
		GoodsCategoryVO goodsCategoryVO = Objects.requireNonNull(BeanUtil.copy(goodsCategory, GoodsCategoryVO.class));
		if(Func.equals(goodsCategory.getParentId(), SystemConstant.TOP_PARENT_ID)) {
			goodsCategoryVO.setParentName(SystemConstant.TOP_PARENT_NAME);
		}else{
			GoodsCategory parent = goodsCategoryService.getById(goodsCategory.getParentId());
			goodsCategoryVO.setParentName(parent.getName());
		}
		return goodsCategoryVO;
	}

	public List<GoodsCategoryVO> listNodeVO(List<GoodsCategory> list) {
		List<GoodsCategoryVO> collect = list.stream().map(item -> BeanUtil.copy(item, GoodsCategoryVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

	public List<GoodsCategoryVO> listNodeLazyVO(List<GoodsCategoryVO> list) {
		return ForestNodeMerger.merge(list);
	}

}
